/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.azienda.moretti.view;

import it.azienda.framework.persistenza.DAOConnectionSingleton;
import it.azienda.framework.utilita.Utilita;
import it.azienda.framework.view.Dettaglio;
import it.azienda.moretti.modello.BeanNews;
import it.azienda.moretti.modello.BeanUtente;
import java.util.ArrayList;
import java.util.HashMap;
import javax.faces.application.FacesMessage;
import javax.faces.component.html.HtmlForm;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import org.apache.myfaces.component.html.ext.HtmlInputText;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author Utente
 */
public class DettaglioNews extends Dettaglio {

    private HtmlForm form = new HtmlForm();
    private BeanNews beanNews;

    public void setBeanNews(BeanNews beanNews) {
        setObj(beanNews);
        this.beanNews = beanNews;
    }

    public BeanNews getBeanNews() {
        return (BeanNews) getObj();
    }

    public HtmlForm getForm() {
        form = (HtmlForm) Utilita.getSession().getAttribute("form");
        if (form == null) {
            form = new HtmlForm();
        }
        Utilita.getSession().removeAttribute("form");
        return form;
    }

    public void setForm(HtmlForm form) {
        setFormObject(form);
        this.form = form;
        Utilita.getSession().setAttribute("form", form);
    }

    public void controllo(ValueChangeEvent event) {
        if (event.getNewValue() != event.getOldValue()) {
            FacesContext ctx = FacesContext.getCurrentInstance();
            HtmlSelectOneMenu selectOneMenu = (HtmlSelectOneMenu) form.findComponent("lista");
            Utilita.getSession().removeAttribute("form");
            String valoreSelect = (String) selectOneMenu.getValue();
            BeanNews bean = new BeanNews();
            if (!valoreSelect.equals("")) {
                try {
                    bean = (BeanNews) bean.dettaglio(new Integer(valoreSelect));
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println("Errore" + e);
                }
                if (bean != null) {
                    BeanUtente login = (BeanUtente) Utilita.getSession().getAttribute("login");
                    HashMap listaOggetti = login.getListaOggetti();
                    for (int i = 0; i < listaOggetti.size(); i++) {
                        BeanNews regione2 = (BeanNews) listaOggetti.get(new Integer(i));
                        if (regione2.getId().intValue() == bean.getId().intValue()) {
                            Utilita.getSession().removeAttribute("posizioneOggetto");
                            Utilita.getSession().setAttribute("posizioneOggetto", new Integer(i));
                        }
                    }
                    this.beanNews = bean;
                    caricaOggetto(bean);
                } else {
                    FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Nessun record presente."));
                }
            }
            Utilita.getRequest().setAttribute("beanNews", bean);
            ctx.renderResponse();
        }
    }

    public void caricaOggetto(BeanNews regione) {
        if (regione.getCodice() != null) {
            ((HtmlInputText) form.findComponent("codice")).setValue(regione.getCodice());
        } else {
            ((HtmlInputText) form.findComponent("codice")).setValue("");
        }
        if (regione.getTitolo() != null) {
            ((HtmlInputText) form.findComponent("titolo")).setSubmittedValue(regione.getTitolo());
        } else {
            ((HtmlInputText) form.findComponent("titolo")).setValue("");
        }
    }

    public boolean controlliPrimaSalvataggio() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        beanNews = (BeanNews) ctx.getExternalContext().getRequestMap().get("beanNews");
        boolean errori = false;

        if (beanNews.getCodice() == null) {
            String query = " select max(regione.codice) from BeanNews regione";
            Session session = null;
            try {
                session = DAOConnectionSingleton.newSession();
                Query q = null;
                q = session.createQuery(query);
                ArrayList listaCodice = (ArrayList) q.list();
                Integer cod = (Integer) listaCodice.get(0);
                if (cod != null) {
                    this.beanNews.setCodice(new Integer(cod.intValue() + 1));
                } else {
                    this.beanNews.setCodice(new Integer(1));
                }
            } catch (Exception e) {
            }
        }
        return false;
    }

    private ArrayList listaNews;

    /**
     * @return the listaNews
     */
    public ArrayList getListaNews() {
        String query = " select bean from BeanNews bean"
                + " where bean.visualizza = 'S'";
        Session session = null;
        try {
            session = DAOConnectionSingleton.newSession();
            Query q = null;
            q = session.createQuery(query);
            listaNews = (ArrayList) q.list();

        } catch (Exception e) {
        }
        if (listaNews == null) {
            listaNews = new ArrayList();
        }
        return listaNews;
    }

    /**
     * @param listaNews the listaNews to set
     */
    public void setListaNews(ArrayList listaNews) {
        this.listaNews = listaNews;
    }
}
